package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * A CONNECT_ACTIVE_IND message.
 */
public class ConnectActiveInd
        extends CapiPassiveMessage
        implements PlciAccessor {

    private static final int OFFSET_PLCI = OFFSET_MESSAGE;
    private static final int OFFSET_STRUCTS = OFFSET_MESSAGE + SIZE_OF_PLCI;

    /**
     * Create a new {@code ConnectActiveInd} based on the
     * specified {@code ByteBuffer}.
     *
     * @param byteBuffer A {@code ByteBuffer} containing a CONNECT_ACTIVE_IND
     *                   message.
     */
    public ConnectActiveInd(final ByteBuffer byteBuffer) {
        super(byteBuffer);
    }

    /** {@inheritDoc} */
    public int getPlci() {
        return getByteBuffer().getInt(OFFSET_PLCI);
    }

    /**
     * Retrieve the connected number.
     *
     * @return The connected number.
     */
    public ConnectedNumber getConnectedNumber() {
        ByteBuffer byteBuffer = getByteBuffer();
        byteBuffer.position(OFFSET_STRUCTS);

        return new ConnectedNumber(byteBuffer);
    }

    /**
     * Retrieve the connected subaddress.
     *
     * @return The connected subaddress.
     */
    public SubAddress getConnectedSubAddress() {
        ByteBuffer byteBuffer = getByteBuffer();
        byteBuffer.position(OFFSET_STRUCTS);
        CapiStruct.skipStruct(byteBuffer);

        return new SubAddress(byteBuffer);
    }

    /**
     * Retrieve the LLC.
     *
     * @return The LLC.
     */
    public Object getLlc() {
        ByteBuffer byteBuffer = getByteBuffer();
        byteBuffer.position(OFFSET_STRUCTS);
        CapiStruct.skipStruct(byteBuffer);
        CapiStruct.skipStruct(byteBuffer);

        return byteBuffer;
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format(
                "%s[%s plci=0x%08x connected#=%s connectedSub#=%s llc=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getPlci(),
                getConnectedNumber(),
                getConnectedSubAddress(),
                getLlc());

        return s;
    }

}
